const rl = @import("raylib");
const std = @import("std");
const map = @import("map.zig");

const min_screen_width = 800;
const min_screen_height = 600;

pub fn main() anyerror!void {
    // RayLib Initialization
    // --------------------------------------
    const config_flags: rl.ConfigFlags = .{ .window_resizable = true };
    rl.setConfigFlags(config_flags);

    // Window Initialization
    //--------------------------------------
    rl.initWindow(min_screen_width, min_screen_height, "Crazy Snake");
    rl.setWindowMinSize(min_screen_width, min_screen_height);
    defer rl.closeWindow();
    rl.setTargetFPS(60);

    // Game Initialization
    //--------------------------------------
    initGame();

    // Main game loop
    //--------------------------------------
    while (!rl.windowShouldClose()) {
        updateGame();
        drawGame();
    }

    // TODO: De-Initialization
}

pub fn initGame() void {}

pub fn updateGame() void {}

pub fn drawGame() void {
    rl.beginDrawing();
    defer rl.endDrawing();

    rl.clearBackground(rl.Color.dark_gray);

    const tile_width = @divFloor(rl.getScreenWidth(), map.width);
    const tile_height = @divFloor(rl.getScreenHeight(), map.height);
    var x: i32 = 0;
    var y: i32 = 0;
    for (map.cells) |row| {
        for (row) |_| {
            rl.drawRectangle(x, y, tile_width, tile_height, rl.Color{ .r = @intCast(rl.getRandomValue(100, 250)), .g = @intCast(rl.getRandomValue(50, 150)), .b = @intCast(rl.getRandomValue(10, 100)), .a = 255 });
            x += tile_width;
        }
        y += tile_height;
        x = 0;
    }
}

pub fn printMap() void {
    for (map, 0..) |row, row_idx| {
        for (row, 0..) |item, col_idx| {
            std.debug.print("map[{}][{}] = {}\n", .{ row_idx, col_idx, item });
        }
    }
}
